/**
 * Created by maple on 2016/6/10.
 */
var request = require('request');
var cheerio = require("cheerio");

var lackUrl = 'http://www.51sfzd.com/CharCollectionSystem/index.php/Report/insuff?&p=';

function lackChar(id, callback) {
    request(lackUrl + id, function (error, res, body) {
        if (!error && res.statusCode == 200) {
            var $ = cheerio.load(body);
            var dl = $('dl[class="normal"]');

            //这里以后要加判断，如果么有数据要处理

            var lackInfo = new Array();
            var num = 0;

            var children = dl[0].children;
            for(var i = 0; i<children.length;i++){
                if(children[i].name == 'dd'){
                
                    var charInfo =children[i].children[0].data.toString().replace('(','');
                    var numInfo =children[i].children[1].children[0].data;
                    lackInfo[num++] = {char:charInfo,num:numInfo};
                }
            }
            var result = {
                status : 1,
                data:lackInfo,
                info:''
            };
            callback(result);
        }
    })
}

module.exports = lackChar;